#pragma hdrstop
/*
  Left off working on a Copy routine using
  Control + Mouse to extend the copy box


*/
#include "Globals.h"
#include <ShellApi.h>


#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TForm1::N3Click(TObject *Sender)
{
//  cur_val = 0xFFFE;
  N3->Caption = "$" + IntToHex( cur_val, 4 );
}



//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TForm1::N5Click(TObject *Sender)
{
  add_val = 0x2000;
  N5->Caption = "$" + IntToHex( add_val, 4 );  
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
  delete TEMP;
  _free( ScrBuf );
  _free( TleBuf );
  _free( MENU[0]);
  _free( MENU[1]);
  _free( CTIL   );
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TForm1::Exit1Click(TObject *Sender)
{
  Form1->Close( );
}



//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  TEMP->PixelFormat = pf16bit;
  //
  for ( int y = 0; y < 30; y++ )
    memset( &map.menu[y], 0x00, 40 << 1 );

  getcwd( buffer, MAXPATH );
  lof[0] = 0x20000;
  lof[1] = 0x20000;

  map.Xdim = 40;
  map.Ydim = 30;
  map.Xpos = 0;
  map.Ypos = 0;

  // Init Tile buffer specs
  tile[0].Xdim = 16;
  tile[0].Ydim = 16;
  tile[1].Xdim = 16;
  tile[1].Ydim = 16;
  // init the map
  for ( int y = 0; y < 16; y++ )
  {
    for ( int x = 0; x < 16; x++ ) {
      tile[0].menu[y][x] = (y << 4) + x;
      tile[1].menu[y][x] = (y << 4) + x;
    }
  }

  box.x =-1;
  box.y =-1;
  box.w = 1;
  box.h = 1;

  iCanDraw    = true;
  cur_val = 0x0000;
  add_val = 0x2800;

  // Try to get some memory
  ScrBuf = (WORD *)malloc( 0x12C00 << 1 );
  TleBuf = (WORD *)malloc( 0x04000 << 1 );
  MENU[0]= (WORD *)malloc( 0x10000 << 1 );
  MENU[1]= (WORD *)malloc( 0x10000 << 1 );
  CTIL   = (WORD *)malloc( 0x10000 << 1 );

  if ( !ScrBuf || !TleBuf || !MENU[0] || !MENU[1] || !CTIL )
  {
    ShowMessage("Fatal Error: Could not allocate needed memory!" );
    Close( );
    return;
  }

  memsetw( ScrBuf, 0x738E, 320 * 240 );
  memsetw(TleBuf, 0, 128 * 128 );
  //
	getcwd(dirc, MAXPATH);
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TForm1::FormPaint(TObject *Sender)
{
  TRect r;
  char buf[256];

  TEMP->Width  = 320;
  TEMP->Height = 240;

  // Draw big map
  for ( int y = 0; y < TEMP->Height; y++)
    memcpyw((WORD *)TEMP->ScanLine[ y ], &ScrBuf[y * 320], 320, 0 );
  //
//  Form1->Canvas->Draw( 0, 0, TEMP );
  Form1->Canvas->StretchDraw( Form1->ClientRect, TEMP );

}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TForm1::OpenMenuMap1Click(TObject *Sender)
{
  #define readw(x) (fgetc(x) + (fgetc(x) << 8))
  char *ext;
  BYTE data;
  WORD valu;
  long lof,
       c;
  FILE *tf;

  Open1->FileName = "";
  Open1->InitialDir = buffer;
  Open1->Filter = "Menu maps ( *.mnu, *.rle )| *.mnu; *.rle";
  Open1->FilterIndex = 0;
  Open1->Title = "Open a Menu Map file...";


  if ( Open1->Execute( ) )
  { //
    strcpy(mapf, Open1->FileName.c_str() );
    ext = strchr( (char *)mapf, '.' );
    //
    tf = fopen(mapf, "rb" );
    if ( !tf )
    {
       ShowMessage("Could not Open Menu Map File.");
       return;
    }
    //
    fseek(tf, 0, SEEK_END);
    lof = ftell(tf);
    fseek(tf, 0, SEEK_SET);
    // load map data
    if ( lof > 0x04B2 ){
      lof = 0x04B0;
      map.Xdim = 40;
      map.Ydim = 30;
    }

    // Check for RLE files
    if ( !strcmpi(ext, ".rle" ) )
    { // We have RLE data
/*      map.Ydim   = 0;
      map.Xdim   = 40;
      map.Xpos   = 0;
      //
      while( lof-- > 0)
      {
        data = fgetc( tf );
        if ( data & 0x80 )
        { // Compression
          data &= 0x7F;
          valu = readw( tf );
          lof -= 2;
          //
          while( data-- )
          { //
            map.menu[map.Ydim][map.Xpos] = valu;
            //
            if ( valu == 0xFFFE ){
              map.Xdim = map.Xpos + 1;
              map.Ydim++;
              map.Xpos = 0;
            } else
              map.Xpos++;
          }
        }
        else
        { // Regular
          data &= 0x7F;
          while( data-- )
          { //
            valu = readw( tf );
            lof -= 2;
            map.menu[map.Ydim][map.Xpos] = valu;
            //
            if ( valu == 0xFFFE ){
              map.Xdim = map.Xpos + 1;
              map.Ydim++;
              map.Xpos = 0;
            } else
              map.Xpos++;
          }
        }
      } // WEND
      map.Ydim++;
*/  }
    else
    { // We have Regular data
        map.Ydim   = fgetc(tf);
        map.Xdim   = fgetc(tf);
        // Read in data
        for ( int y = 0; y < map.Ydim; y++ )
        { //
          memset(&map.menu[y], 0, 40);
          fread( &map.menu[y], 2, map.Xdim, tf );
        }
    }// END IF
    fclose(tf);

    iCanDraw           = false;
    //
    memsetw( ScrBuf, 0x738E, 320 * 240 );
    MenuMap( ScrBuf, &map , 320, false);
    iCanDraw           = true;

    Form1->FormPaint( NULL );

    c = strlen(mapf) - 1;
    while( mapf[c] != '\\' && mapf[c] != '/' && c > 0 ) c--;
    Form1->Caption = "Menu Editor - " + AnsiString((char *)&mapf[c+1] );
  }
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TForm1::TileOpen( FILE *tf, char which )
{
  //
  fseek(tf, 0, SEEK_END);
  lof[which] = ftell(tf);
  fseek(tf, 0, SEEK_SET);

  if ( lof[which] > 0x20000 )
  {
//    ShowMessage("Tile file has more than 255 tiles. only 255 were loaded" );
    lof[which] = 0x20000;
  }
  //
  memsetw(MENU[which], 0, 0x10000);
  fread(MENU[which], 1, lof[which], tf );
  //
  for ( int y = 0; y < 64; y++ )
  {
     for ( int x = 0; x < 16; x++ )
       tile[which].menu[y][x] = (y << 4) + x;
  }
  //
  tile[which].Xdim = 16;
//  tile[which].Ydim = 16;
  tile[which].Ydim = ((lof[which] >> 6) >> 4);
  //
  MenuMap( ScrBuf, &map, 320, false );
  MenuMap( TleBuf, &tile[which], 128, true );
  TileForm->FormPaint( NULL );
  FormPaint( NULL );
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
  X >>= 3;
  Y >>= 3;

  if ( (X < 0 || Y < 0) || (X >= map.Xdim || Y >= map.Ydim) ) return;

  if ( Button == mbLeft )
  { // Reset the Tile Copy sizes
    if ( til.w > 1 || til.h > 1 )
    {
      til.w = til.h = 1;
      til.y = -1; til.x = -1;
      TileForm->FormPaint( NULL );
    }

    //
    cur_val     = map.menu[Y][X] & 0x03FF;
    add_val     = map.menu[Y][X] & 0xFC00;
    N3->Caption = "$" + IntToHex( cur_val, 4 );
    N5->Caption = "$" + IntToHex( add_val, 4 );
    memcpyw(cur_tile, &MENU[cur_tle][ cur_val << 6 ], 64, 0);
    /////////////////////////////
    // for data copy           //
    /////////////////////////////
    if ( box.w > 1 || box.h > 1 )
    {
      for ( int y = 0; y < box.h; y++ )
      { // bounds checkin
        if ( (y + Y) >= 30 ) break;
        //
        for ( int x = 0; x < box.w; x++ )
        { //
          if ( (x + X) >= 40 ) break;
          //
          copy[y][x] = map.menu[Y + y][X + x];
        }
      }// NEXT
    }// END IF

    MiniForm->FormPaint( NULL );
  }
  else
  {
    // We only wanna change the values attributes!
    // using the default setting. Fast way to do it!
    if ( KEY_DOWN(VK_SHIFT) )
    { //
      map.menu[Y][X] = (map.menu[Y][X] & 0x03FF) | add_val;
      // Fill all others as well
      if ( box.w > 1 || box.h > 1 )
      { //
        for ( int y = 0; y < box.h; y++ ){
          // bounds checkin
          if ( (y + Y) >= 30 ) break;
          //
          for ( int x = 0; x < box.w; x++ ){
            //
            if ( (x + X) >= 40 ) break;
            //
            map.menu[Y + y][X + x] = (map.menu[Y + y][X + x] & 0x03FF) | add_val;
          }
        }// NEXT
      }// END IF
      //
      return;
    }

    /////////////////////////////
    // for data copy           //
    /////////////////////////////
    if ( box.w > 1 || box.h > 1 )
    {
      for ( int y = 0; y < box.h; y++ )
      { // bounds checkin
        if ( (y + Y) >= 30 ) break;
        //
        for ( int x = 0; x < box.w; x++ )
        { //
          if ( (x + X) >= 40 ) break;
          //
          map.menu[Y + y][X + x] = copy[y][x];
        }
      }// NEXT
    }// END IF

    map.menu[Y][X] = (cur_val | add_val);
    N3->Caption = "$" + IntToHex( cur_val, 4 );
    N5->Caption = "$" + IntToHex( add_val, 4 );    
    MenuMap( ScrBuf, &map, 320, false );
    FormPaint( NULL );
  }
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TForm1::SaveMenuMap1Click(TObject *Sender)
{
  FILE *tf;

  Save1->FileName = "";
  Save1->DefaultExt = ".mnu";
  Save1->InitialDir = buffer;
  Save1->Filter = "Menu Map files ( *.mnu ) | *.mnu";
  Save1->FilterIndex = 0;
  Save1->Title = "Save a Menu Map file...";

  if ( !mapf[0] )
  {
    if ( Save1->Execute( ) )
    {
      strcpy(mapf, Save1->FileName.c_str() );
    }else
      return;
  }
  //
  tf = fopen(mapf, "wb" );
  if ( !tf )
  {
     ShowMessage("Could not Save Menu Map File.");
     return;
  }
  //
  fputc( map.Ydim , tf );
  fputc( map.Xdim , tf );
  //
  for ( int i = 0; i < map.Ydim; i++ )
    fwrite( &map.menu[i], 2, map.Xdim, tf );

  fclose( tf );
  ShowMessage("Menu Map File Saved");
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TForm1::SaveMenuTiles1Click(TObject *Sender)
{
  char buf[384];
  int k;
  FILE *tlf;

  Save1->FileName = "";
  Save1->InitialDir = buffer;
  Save1->Filter = "Tile files ( *.16b ) | *.16b";
  Save1->FilterIndex = 0;
  Save1->Title = "Save a TILE file...";

  if ( !tilf[cur_tle][0] )
  {
    if ( Save1->Execute( ) )
    {
      strcpy(tilf[cur_tle], Save1->FileName.c_str() );
    }else
      return;
  }

  k = Application->MessageBox( "Save the tiles?", "Save Tile File...", MB_YESNOCANCEL );
  //
  if ( k == IDCANCEL ) return;
  //
  tlf = fopen(tilf[cur_tle], "wb" );
  if ( !tlf )
  {
     ShowMessage("Could not Save Tile File.");
     return;
  }

  if ( k == ID_YES  ){
    fwrite(MENU[cur_tle], 1, lof[cur_tle], tlf );
    ShowMessage("Tile File Saved");
  }
  //
//  fwrite(&MENU[cur_tle], 2, lof[cur_tle], tf );
  fclose( tlf );

}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift, int X, int Y)
{
  #define scale 4
  TBOX  t;
  TRect r,
        b;
  WORD TILE[64];

  Form1->SetFocus( );

  // Epand Mouse box
  if ( KEY_DOWN(VK_CONTROL) )
  {
    t.w = box.w;
    t.h = box.h;
    //
    box.w = ((X >> 3) - box.x) + 1;
    box.h = ((Y >> 3) - box.y) + 1;
    if ( box.w <= 0 || box.h <= 0 ){
      box.w = t.w;
      box.h = t.h;
      return;
    }
    //
    FormPaint( NULL );

    r = Rect( box.x << 3 , box.y << 3, (box.x << 3) + (box.w << 3), (box.y << 3) + (box.h << 3) );
    //
    Form1->Canvas->Brush->Color = clWhite;
    Form1->Canvas->FrameRect( r );
    return;
  }

  //
  if ( KEY_DOWN(VK_LBUTTON) ) FormMouseDown(Sender, mbLeft , Shift, X, Y);
  if ( KEY_DOWN(VK_RBUTTON) ) FormMouseDown(Sender, mbRight, Shift, X, Y);

  // save the last position
  r = Rect( X & 0xFFF8 , Y & 0xFFF8, (X  & 0xFFF8) + (box.w<<3), (Y & 0xFFF8) + (box.h<<3) );

  X >>= 3;
  Y >>= 3;

  // Test for boundaries
  if ( ((X >= 40 || Y >= 30) || (X < 0 || Y < 0)) ) return;
  //
  if ( box.y != -1 && box.x != -1 )
  { //
    while( (box.x + box.w) > 40 )
      box.x--;
    while( (box.y + box.h) > 30 )
      box.y--;

    TEMP->Width  = box.w << 3;
    TEMP->Height = box.h << 3;
    for ( int i = 0; i < TEMP->Height; i++ )
      memcpyw((WORD *)TEMP->ScanLine[ i ], &ScrBuf[(((box.y << 3)  + i) * 320) + (box.x << 3)], TEMP->Width, 0 );
    Form1->Canvas->Draw( box.x << 3, box.y << 3, TEMP );
  }

  //
  Form1->Canvas->Brush->Color = clWhite;
  Form1->Canvas->FrameRect( r );
  //
  box.y = Y;
  box.x = X;
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TForm1::SwapTileFiles1Click(TObject *Sender)
{
  int c;

  cur_tle ^= 1;
  MenuMap( TleBuf, &tile[cur_tle], 128, true );
  MenuMap( ScrBuf, &map, 320, false );
  //
  TileForm->FormPaint( NULL );
  FormPaint( NULL );

  c = strlen(tilf[cur_tle]) - 1;
  while( tilf[cur_tle][c] != '\\' && tilf[cur_tle][c] != '/' && c > 0 ) c--;
  TileForm->Caption = "Tile Viewer - " + AnsiString((char *)&tilf[cur_tle][c+1] );

}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TForm1::TileFile11Click(TObject *Sender)
{
  int c;
  FILE *tf;

  Open1->FileName = "";
  Open1->InitialDir = buffer;
  Open1->Filter = "Tile files ( *.16b ) | *.16b";
  Open1->FilterIndex = 0;
  Open1->Title = "Open a TILE file...";


  if ( Open1->Execute( ) )
  {
    strcpy(tilf[0], Open1->FileName.c_str() );
    //
    tf = fopen(tilf[0], "rb" );
    if ( !tf )
    {
       ShowMessage("Could not Open Tile File.");
       return;
    }

    TileOpen( tf, 0 );
    fclose(tf);

    c = strlen(tilf[0]) - 1;
    while( tilf[0][c] != '\\' && tilf[0][c] != '/' && c > 0) c--;
    TileForm->Caption = "Tile Viewer - " + AnsiString((char *)&tilf[0][c+1] );
  }
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TForm1::TileFile21Click(TObject *Sender)
{
  FILE *tf;

  Open1->FileName = "";
  Open1->InitialDir = buffer;
  Open1->Filter = "Tile files ( *.16b ) | *.16b";
  Open1->FilterIndex = 0;
  Open1->Title = "Open a TILE file...";


  if ( Open1->Execute( ) )
  {
    strcpy(tilf[1], Open1->FileName.c_str() );
    //
    tf = fopen(tilf[1], "rb" );
    if ( !tf )
    {
       ShowMessage("Could not Open Tile File.");
       return;
    }

    TileOpen( tf, 1 );
    fclose(tf);
  }
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
/*
  if ( Key == VK_UP   ) rat.y--;
  if ( Key == VK_DOWN ) rat.y++;
  if ( Key == VK_LEFT ) rat.x--;
  if ( Key == VK_RIGHT) rat.x++;
  //
  rat.y &= 0xFFFF;
  rat.x &= 0xFFFF;
  MiniForm->FormPaint( NULL );
*/
/*
  if ( KEY_DOWN(VK_CONTROL) )
  {
    switch( Key )
    {
       case VK_LEFT : if ( map.Xpos ) map.Xpos--; break;
       case VK_RIGHT: if ((map.Xpos + map.Xdim + 1) <= 40) map.Xpos++; break;
       //=============================
       case VK_UP   : if ( map.Ypos ) map.Ypos--; break;
       case VK_DOWN : if ((map.Ypos + map.Ydim + 1) <= 30) map.Ypos++; break;
       default      : Key = 0; return;
    }

  }
  else
*/
  // Standard keys
  {
    switch( Key )
    {
       case VK_1    : add_val ^= 0x8000; Key = 0; break;
       case VK_2    : add_val ^= 0x4000; Key = 0; break;
       case VK_3    : add_val ^= 0x2000; Key = 0; break;
       case VK_4    : add_val ^= 0x1000; Key = 0; break;
       case VK_5    : add_val ^= 0x0800; Key = 0; break;
       case VK_6    : add_val ^= 0x0400; Key = 0; break;
       case VK_LEFT : if ( map.Xdim > 1 ) map.Xdim--; break;
       case VK_RIGHT: if ( map.Xdim < 40) map.Xdim++; break;
       //=============================
       case VK_UP   : if ( map.Ydim > 1 ) map.Ydim--; break;
       case VK_DOWN : if ( map.Ydim < 30) map.Ydim++; break;
       default      : Key = 0; return;
    }
  }// END IF

  if ( Key )
  {
    memsetw( ScrBuf, 0x738E, 320 * 240 );
    MenuMap( ScrBuf, &map, 320, false );
    FormPaint( NULL );
  }
  //  
  N3->Caption = "$" + IntToHex( cur_val, 4 );
  N5->Caption = "$" + IntToHex( add_val, 4 );
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TForm1::NewMap1Click(TObject *Sender)
{
  memset( map.menu, 0x00, (40 * 30) << 1 );
  memset(mapf, 0, 260);
  map.Xdim  = 40;
  map.Ydim  = 30;
  map.Xpos  = 0;
  map.Ypos  = 0;
  //
  memsetw( ScrBuf, 0x738E, 320 * 240 );
  MenuMap( ScrBuf, &map, 320, false );
  FormPaint( NULL );
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TForm1::SaveMapAs1Click(TObject *Sender)
{
  int c;

  Save1->FileName = "";
  Save1->DefaultExt = ".mnu";
  Save1->InitialDir = buffer;
  Save1->Filter = "Menu Map files ( *.mnu ) | *.mnu";
  Save1->FilterIndex = 0;
  Save1->Title = "Save a Menu Map file...";

  if ( Save1->Execute( ) )
  {
    strcpy(mapf, Save1->FileName.c_str() );
    SaveMenuMap1Click(Sender);
  }

  c = strlen(mapf) - 1;
  while( mapf[c] != '\\' && mapf[c] != '/' && c > 0 ) c--;
  Form1->Caption = "Menu Editor - " + AnsiString((char *)&mapf[c+1] );
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TForm1::Convert256colorfileto16Bit1Click(TObject *Sender)
{
  LONG lof,
       pos;
  char file[520],
       *p;
  BYTE *data;
  FILE *tf;

  Open1->FileName = "";
  Open1->InitialDir = buffer;
  Open1->Filter = "All files ( *.* ) | *.*";
  Open1->FilterIndex = 0;
  Open1->Title = "Open a file to convert...";


  if ( Open1->Execute( ) )
  { //
    strcpy( file, Open1->FileName.c_str() );
    //
    tf = fopen( file, "rb" );
    if ( !tf )
    {
       ShowMessage("Could not Open Tile File.");
       return;
    }
    //
    fseek( tf, 0, SEEK_END );
    lof = ftell( tf );
    fseek( tf, 0, SEEK_SET );
    //
    data = (BYTE *)malloc( lof );
    if ( !lof ){
      ShowMessage("Fatal Error: Could not allocate memory..." );
      return;
    }
    //
    fread( data, 1, lof, tf );
    fclose( tf );

    p = strchr( file, '.' );
    if ( p )
      strcpy(p, ".16b" );
    else
      strcat( file, ".16b" );
    //
    tf = fopen( file, "wb" );
    if ( !tf ){
      ShowMessage("Error: Could not open output file." );
      return;
    }

    pos = 0;
    while( pos < lof ){
      fputc( DOS565[ data[pos] ] >> 0, tf );
      fputc( DOS565[ data[pos] ] >> 8, tf );
      pos++;
    }
    fclose(tf);
    //
    ShowMessage("File conversion completed!" );
  }
}


////////////////////////////////////////
//                                    //
////////////////////////////////////////
void __fastcall TForm1::CallExternClick(TObject *Sender)
{
  FILE *tf;
  char *arg[] =
  {
    "%ROMFILE",
    "%SCRIPTFILE"
  };

  int  len,
       pos;
  char ShortAppName[512];
  HINSTANCE hinst;

  Open1->InitialDir = dirc;
  Open1->FileName = "";
  Open1->Filter = "All files (*.*)|*.*";

  //
  if ( Open1->Execute( ) )
  { //
    if ( !exef[0] )
    {
      ShowMessage("Error: No Inserter Application Loaded!");
      return;
    }

    //------------------------------------//
    // Make the Name capable of Using DOS //
    //------------------------------------//
    len = strlen( exef );
    GetShortPathName( exef, ShortAppName, 512 );

    // Execute what the hell they want!
    hinst = ShellExecute
            ( //
              Form1->Handle,
              "open",
              ShortAppName,
              Open1->FileName.c_str( ),
              NULL,
              SW_SHOWNORMAL
            );

    if( (int) hinst < 33 ){
      ShowMessage( "Fatal Error: Could not execute the needed program!\n"
                   "Make sure the program is in the location."
                 );
      return;
    }
  }
}


////////////////////////////////////////
//                                    //
////////////////////////////////////////
void __fastcall TForm1::LoadExternClick(TObject *Sender)
{
  LONG lof;
  FILE *tf;
  int  c;
  char buf[1024],
       file[1024];

  Open1->FileName    = "";
  Open1->InitialDir  = buffer;
  Open1->Filter      = "Executable files ( *.exe, *.com)| *.exe; *.com|All files ( *.* )| *.*";
  Open1->FilterIndex = 0;
  Open1->Title       = "Open an Executable file...";
  //
  if ( Open1->Execute( ) )
  { //
    GetShortPathName( Open1->FileName.c_str( ), exef, 1023 );
  }
  //
  c = strlen( exef );
  while( exef[c] != '\\' && exef[c] != '/' )
    c--;
  //
  c++;

  sprintf( buf, "&Call External Program \"%s\"", &exef[c] );
  CallExtern->Caption = AnsiString( buf );
  CallExtern->Enabled = true;
}




